/*下载链接
彭济
9-23*/
import Sequelize from 'sequelize'
import sequelize from './../../database'

const File = sequelize.define('sys_download',{
	id:{
		type: Sequelize.UUIDV4,
        defaultValue: Sequelize.UUIDV4,
        unique: true,
        allowNull: false,
        primaryKey:true
	},
	download_title:{
		type: Sequelize.STRING,
		allowNull:false
	},
	download_subtitle:{
		type: Sequelize.STRING,
		allowNull:true
	},
	type:{
		type: Sequelize.INTEGER,
		allowNull:false
	},
	download_version:{
		type: Sequelize.STRING,
		allowNull:false
	},
	download_url:{
		type: Sequelize.STRING,
		allowNull:false
	},
	is_enable:{
		type: Sequelize.INTEGER,
		allowNull:false
	}
},{
	freezeTableName:false,
    updatedAt:'updated_at',
    createdAt:'created_at',
	tableName:'sys_download'
});

class DownloadModel{
	//新增下载链接
	insertUrl(record){
		return File.create(record);
	}
	//根据id查找url
	urlFindById(id){
		return File.findById(id);
	}
	findAllUrl(record){
		return File.findAll();
	}
	//分页
	urlPaging(record){
		return File.findAndCount({
            limit:record.page_size,
            offset: record.page_size * (record.page_no -1)
        })
	}
	//删除url
	urlDelById(id){
		return File.destroy({
			where:{
				id:id,
			}
		});
	}
	//修改url
	urlUpdateById(record){
		return File.update(record,{
			where:{
				id:record.id
			},
			fields:['download_title','download_subtitle','type','download_version','download_url','is_enable']
		});
	}
	updateByEnable(record){
		return File.update({is_enable:0},{
			where:{
				is_enable:1,
				type:record.type
			},
			fields:['is_enable']
		});
	}
	findByEnable(is_enable){
		return File.findAll({
			where:{
				is_enable:1,
			},
		});
	}
}
module.exports = new DownloadModel();